//# sourceURL=http://uMapApp/Module/Bridge/BridgeProfile
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function BridgeProfile(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        if (_this.position) {

            _this.startStakeNum = StakeNum.transform(_this["开始桩号"]);
            _this.stopStakeNum = StakeNum.transform(_this["结束桩号"]);

            _this.title = _this["名称"];
            _this.searchKey = _this["名称"] + " " + _this["类型"] + " " + _this["朝向"];
            _this.sortKey = _this.startStakeNum.distance;

            _this.layer = new Layer({
                "dataSource": _this.dataSource,
                "app"       : _this.app
            });

            _this.layer.dataSourceView.on("loaded", function () {
                _this.layer.dataSourceView.close();

                _this.features = {};

                _this.layer.dataSourceView.tileSet.object.tileLoad.addEventListener(function (tile) {

                    if (tile.content.innerContents) {

                        $.each(tile.content.innerContents, function (i, innerContent) {

                            for (var j = 0; j < innerContent.featuresLength; j++) {
                                var feature = innerContent.getFeature(j);
                                if (feature && feature.getProperty) {
                                    var DbId = parseInt(feature.getProperty("DbId"));
                                    _this.features[DbId] = _this.features[DbId] || [];
                                    _this.features[DbId].push(feature);
                                }
                            }

                        });


                    } else if (tile.content) {

                        for (var i = 0; i < tile.content.featuresLength; i++) {
                            var feature = tile.content.getFeature(i);
                            if (feature && feature.getProperty) {
                                var DbId = parseInt(feature.getProperty("DbId"));
                                _this.features[DbId] = _this.features[DbId] || [];
                                _this.features[DbId].push(feature);
                            }
                        }
                    }

                });


            });

            var styles = {
                normal   : ["#FFF", 1],
                highlight: ["#FF0", 1],
                active   : ["#FF0", 1]
            };

            // _this.layer.dataSourceView.tileSet.on("mouseOver", function (event) {
            //
            //     if (event.pickObjectStop) {
            //         var feature = event.pickObjectStop;
            //         if (feature.getProperty("DbId")) {
            //             _this.layer.dataSourceView.setStyle([{
            //                 tag   : "DbId",
            //                 value : feature.getProperty("DbId"),
            //                 style : styles.highlight
            //             }]);
            //         }
            //     }
            //
            // });
            //

            //todo 预载json，通过ELEMENT_ID高亮

            _this.layer.dataSourceView.tileSet.on("click", function (event) {

                var dataSourceView = _this.layer.dataSourceView;

                if (event.pickObject) {
                    var feature = event.pickObject;
                    if (feature.getProperty("DbId")) {

                        var lastIndex = dataSourceView.url.lastIndexOf("/");
                        var basePath = lastIndex == -1 ? "." : dataSourceView.url.substr(0, lastIndex);

                        $.get(basePath + "/info/" + parseInt(feature.getProperty("DbId") / 100) + ".json", function (json) {

                            if (json.data && json.data[feature.getProperty("DbId")]) {

                                if (_this.features[feature.getProperty("DbId")]) {

                                    if (_this.activeFeatures && _this.activeFeatures !== _this.features[feature.getProperty("DbId")]) {
                                        $.each(_this.activeFeatures, function (i, feature) {
                                            feature.color = new uMap.Color(styles.normal);
                                        });
                                    }

                                    $.each(_this.features[feature.getProperty("DbId")], function (i, feature) {
                                        feature.color = new uMap.Color(styles.active);
                                    });

                                    _this.activeFeatures = _this.features[feature.getProperty("DbId")];

                                }


                                var properties = json.data[feature.getProperty("DbId")];
                                // console.log(properties.categories[6].props.values[0]);
                                var ELEMENT_ID = properties.categories[6].props.values[0];
                                _this.trigger("elementSelect", ELEMENT_ID);
                            }

                        });


                    }
                }

            });

            _this.marker = new uMap.Entity({
                objectOption: {
                    position : _this.position,
                    billboard: new uMap.BillboardGraphic({
                        objectOption: {
                            image         : _this.app.icons.getIconMarker(_this.app.icons.bridge),
                            verticalOrigin: "BOTTOM"
                            // , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    }),
                    label    : new uMap.LabelGraphic({
                        objectOption: {
                            text                      : _this["名称"],
                            verticalOrigin            : "TOP"
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    })
                }
            });

            _this.marker.on("click", function () {
                _this.trigger("select", _this);
            });

            _this.layer.dataSources.entities.add(_this.marker.object);
        }


    }

    var _class = BridgeProfile;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"  : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close" : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle": {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active": {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.layer) {
                            _this.layer.active();
                            _this.layer.dataSourceView.open();
                        }

                        _this.marker.label.fillColor = new uMap.Color(["#ff0"]);
                        _this.marker.label.scale = 1.2;
                        _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.app.icons.bridge, true);

                        _this.trigger("active");

                    } else {

                        if (_this.layer) {
                            _this.layer.dataSourceView.close();
                        }

                        _this.marker.label.fillColor = new uMap.Color(["#FFF"]);
                        _this.marker.label.scale = 1;
                        _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.app.icons.bridge);

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        }
    });

    return _class;

});